package com.bbs.detai1713.controller;

import com.bbs.detai1713.service.DataService;
import com.bbs.detai1713.tool.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
public class ImageController {
    @Autowired
    DataService dataService;

    @PostMapping("/uploadImage")
    public ResultInfo uploadImage(@RequestParam("file") MultipartFile file, @AuthenticationPrincipal UserDetails userDetails) throws IOException {
        log.info("上传图片了:{}", file);
        String fileName = dataService.savePicByFormData(file, userDetails.getUsername());
        return ResultInfo.success(fileName, "上传成功!");
    }

    @PostMapping("/uploadImageReply")
    public ResultInfo uploadImageReply(@RequestParam("file") MultipartFile file, @AuthenticationPrincipal UserDetails userDetails) throws IOException {
        log.info("上传图片了:{}", file);
        String fileName = dataService.savePicByFormDataReply(file, userDetails.getUsername());
        return ResultInfo.success(fileName, "上传成功!");
    }
}
